﻿using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using Maticsoft.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace FangChanCrm.Services.Realization
{
    public class t_GangWeiBiaoService : It_GangWeiBiaoService
    {
        public readonly MyContext _Context;

        public t_GangWeiBiaoService(MyContext context)
        {
            _Context = context;
        }

        public t_GangWeiBiao Add(t_GangWeiBiao t_GangWeiBiao)
        {
            throw new NotImplementedException();
        }

        public bool Delete(int id)
        {
            throw new NotImplementedException();
        }

        public t_GangWeiBiao GetById(int id)
        {
            return _Context.t_GangWeiBiao.FirstOrDefault(x => x.Id == id);
        }

        public List<t_GangWeiBiao> GetList()
        {
            return _Context.t_GangWeiBiao.Include(x => x.t_BuMenBiao).Include(x => x.t_BuMenBiao.t_GongSiBiao).ToList();
        }
        public string ReturnWhere(t_GangWeiBiao t_GangWeiBiao)
        {
            throw new NotImplementedException();
        }

        public List<t_BuMenBiao> BangDingBuMen()
        {
            return _Context.t_BuMenBiao.Where(x => x.BuMenMingCheng != null && x.ZhuangTai != "停用").ToList();
        }
        public string GangWeiAdd(t_GangWeiBiao t_GangWeiBiao)
        {
            _Context.t_GangWeiBiao.Add(t_GangWeiBiao);
            if (_Context.SaveChanges() > 0)
            {
                return "success";
            }
            else
            {
                return "error";
            }
        }
        public List<t_GangWeiBiao> listshuju()
        {
            return _Context.t_GangWeiBiao.Include(x => x.t_BuMenBiao).ToList();
            //return Helper.ListToJsonAddFenYe(_Context.t_BuMenBiao.Include(x=>x.t_GongSiBiao).Include(x=>x.t_RenYuanBiao).Where(ReturnWhere1(bumen)).ToList(), start, limit);
        }
        public List<t_GangWeiBiao> huibangQiYelist(int id)
        {
            return _Context.t_GangWeiBiao.Where(x => x.Id == id).ToList();
        }

        public t_GangWeiBiao ChaXunYiGe(int id)
        {
            t_GangWeiBiao t_GangWeiBiao = _Context.t_GangWeiBiao.Where(x => x.Id == id).ToList()[0];
            return t_GangWeiBiao;
        }
        public string ReturnWhere1(t_GangWeiBiao t_GangWeiBiao)
        {
            string returnstr = "Id != 1";
            if (t_GangWeiBiao.BeiZhu1 != null)
            {
                returnstr = returnstr + " and BeiZhu1.Contains(\"" + t_GangWeiBiao.BeiZhu1 + "\")";
            }
            if (t_GangWeiBiao.BeiZhu2 != null)
            {
                returnstr = returnstr + " and BeiZhu2.Contains(\"" + t_GangWeiBiao.BeiZhu2 + "\")";
            }
            if (t_GangWeiBiao.BeiZhu3 != null)
            {
                returnstr = returnstr + " and BeiZhu3.Contains(\"" + t_GangWeiBiao.BeiZhu3 + "\")";
            }
            if (t_GangWeiBiao.BeiZhu4 != null)
            {
                returnstr = returnstr + " and BeiZhu4.Contains(\"" + t_GangWeiBiao.BeiZhu4 + "\")";
            }
            if (t_GangWeiBiao.BeiZhu5 != null)
            {
                returnstr = returnstr + " and BeiZhu5.Contains(\"" + t_GangWeiBiao.BeiZhu5 + "\")";
            }
            if (t_GangWeiBiao.BeiZhu6 != null)
            {
                returnstr = returnstr + " and BeiZhu6.Contains(\"" + t_GangWeiBiao.BeiZhu6 + "\")";
            }
            if (t_GangWeiBiao.BeiZhu7 != null)
            {
                returnstr = returnstr + " and BeiZhu7.Contains(\"" + t_GangWeiBiao.BeiZhu7 + "\")";
            }
            if (t_GangWeiBiao.BeiZhu8 != null)
            {
                returnstr = returnstr + " and BeiZhu8.Contains(\"" + t_GangWeiBiao.BeiZhu8 + "\")";
            }
            if (t_GangWeiBiao.BeiZhu9 != null)
            {
                returnstr = returnstr + " and BeiZhu9.Contains(\"" + t_GangWeiBiao.BeiZhu9 + "\")";
            }
            if (t_GangWeiBiao.BeiZhu10 != null)
            {
                returnstr = returnstr + " and BeiZhu10.Contains(\"" + t_GangWeiBiao.BeiZhu10 + "\")";
            }
            if (t_GangWeiBiao.GangWeiMingCheng != null)
            {
                returnstr = returnstr + " and GangWeiMingCheng.Contains(\"" + t_GangWeiBiao.GangWeiMingCheng + "\")";
            }
            if (t_GangWeiBiao.GangWeiLeiXing != null)
            {
                returnstr = returnstr + " and GangWeiLeiXing.Contains(\"" + t_GangWeiBiao.GangWeiLeiXing + "\")";
            }
            if (t_GangWeiBiao.BuMenId != null)
            {
                returnstr = returnstr + " and BuMenId==" + t_GangWeiBiao.BuMenId + "";
            }
            if (t_GangWeiBiao.DengJi != null)
            {
                returnstr = returnstr + " and DengJi ==" + t_GangWeiBiao.DengJi + "";
            }
            return returnstr;
        }

        public string GetListWithFenYe(t_GangWeiBiao t_GangWeiBiao, int start, int limit, string dir, int column)
        {
            var list = _Context.t_GangWeiBiao.Include(x => x.t_BuMenBiao).Where(ReturnWhere1(t_GangWeiBiao)).ToList();
            switch (column)
            {
                case 0:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.GangWeiMingCheng).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.GangWeiMingCheng).ToList();
                    }
                    break;
                case 1:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.t_BuMenBiao.BuMenMingCheng).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.t_BuMenBiao.BuMenMingCheng).ToList();
                    }
                    break;
                case 2:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.GangWeiLeiXing).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.GangWeiLeiXing).ToList();
                    }
                    break;
                case 3:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.DengJi).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.DengJi).ToList();
                    }
                    break;
                default:
                    break;
            }
            return Helper.ListToJsonAddFenYe(list, start, limit);
        }
    }
}
